#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <sys/time.h>

/* 	Calculating the pi value using Monte Carlo simulation 	*/

int main (int argc, char ** argv)
{	
	if(argc != 2) {
		printf("Usage: ./monte-carlo-seq <number_of_iteractions>\n");
		exit(1);
	}

	/*	initializing variables	*/
	long long n = atoll(argv[1]);
	long long count = 0, i;

	//	seed
	struct timeval time;
	struct drand48_data randBuffer;
	gettimeofday(&time, NULL);
	srand48_r(time.tv_usec * 1, &randBuffer);

	for(i = 0; i < n; i++) {
		double x, y;

		drand48_r(&randBuffer, &x);
		drand48_r(&randBuffer, &y);
		x = 2 * x - 1;
		y = 2 * y - 1;

		// condition to get one more point
		if(x * x + y * y <= 1)
			count++;
	}

	double pi = 4.0 * (double) count / (double) n ;
	printf("%.6lf\n", pi);

	return 0;
}
